package com.snowdream.gravatar4androiddemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.google.gson.Gson;
import com.loopj.android.image.SmartImageView;
import com.snowdream.gravatar4android.Gravatar;
import com.snowdream.gravatar4android.GravatarType;
import com.snowdream.gravatar4android.data.GravatarProfile;
import com.snowdream.gravatar4android.exception.GravatarException;
import com.snowdream.gravatar4android.listener.AbstractGravatarListener;

public class MainActivity extends Activity {
    private SmartImageView myImage = null;
    
    private TextView mTextView = null;
    
    private Gson mGson = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();

        //Get the gravatar instance
        Gravatar gravatar = Gravatar.getInstance();
        
        //set the email address
        try {
            gravatar.setEmail("beau@dentedreality.com.au");
        } catch (GravatarException e1) {
            e1.printStackTrace();
        }
        
        //use https:// protocol
        gravatar.setSecure(true);
        
        //get the gravatar url
        myImage.setImageUrl(gravatar.getGravatarUrl(GravatarType.JPEG));

        //get the profile asynchronous
        gravatar.asyncGetProfile(new AbstractGravatarListener<GravatarProfile>() {

            @Override
            public void onStart() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onGravatarException(GravatarException e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(GravatarProfile obj) {
                mTextView.setText(mGson.toJson(obj));

            }
        });

    }
    
    
    public void initView() {
        myImage = (SmartImageView)this.findViewById(R.id.my_image);
        mTextView = (TextView)this.findViewById(R.id.my_textview);
    }
    
    public void initData() {
        mGson = new Gson();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}
